import os

from SCons.Script import *

class ARCH:
    HOST    = 'host'
    ARM_M   = 'arm-m'
    ARM_A   = 'arm'
    AARCH64 = 'aarch64'


class CPU:
    HOST    = 'host'
    # arm-m
    M0      = 'cortex-m0'
    M4      = 'cortex-m4'
    # arm-a
    A7      = 'cortex-a7'
    # aarch64
    A53     = 'cortex-a53'


EXEC_PATH_ARM_M = r'C:\dev\gnu\gcc-arm-none-eabi-10.3-2021.07\bin'
PREFIX_ARM_M = 'arm-none-eabi-'

EXEC_PATH_ARM = r'C:\dev\gnu\gcc-arm-10.3-2021.07-mingw-w64-i686-arm-none-eabi\bin'
PREFIX_ARM = 'arm-none-eabi-'

EXEC_PATH_AARCH64 = r'C:\dev\gnu\gcc-arm-10.3-2021.07-mingw-w64-i686-aarch64-none-elf\bin'
PREFIX_AARCH64 = 'aarch64-none-elf-'


def GetCompiler(cpu='', debug=0, **params):

    CC      = 'gcc'
    CXX     = 'g++'
    AS      = 'gcc'
    AR      = 'ar'
    LINK    = 'gcc'
    SIZE    = 'size'
    OBJDUMP = 'objdump'
    OBJCPY  = 'objcopy'
    READELF = 'readelf'

    EXEC_PATH   = ''
    PREFIX      = ''

    CFLAGS = '-Wall'
    AFLAGS = '-c -x assembler-with-cpp'
    LFLAGS = ''

    if debug == 0:
        CFLAGS += ' -O2'
    else:
        CFLAGS += ' -O0 -gdwarf-2'
        AFLAGS += ' -gdwarf-2'

    arch = ARCH.HOST
    DEVICE = ''

    if cpu == CPU.M0:
        arch    = ARCH.ARM_M
        DEVICE  = ' -mcpu=cortex-m0'
    elif cpu == CPU.M4:
        arch    = ARCH.ARM_M
        DEVICE  = ' -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard'
    elif cpu == CPU.A7:
        arch    = ARCH.ARM_A
        DEVICE  = ' -mcpu=cortex-a7'
    elif cpu == CPU.A53:
        arch    = ARCH.AARCH64
        DEVICE  = ' -mcpu=cortex-a53'

    if 'bare' in params:
        if params['bare']:
            DEVICE += ' -nostartfiles'
            # DEVICE += ' -nostdlib'
    
    CFLAGS += DEVICE
    LFLAGS += DEVICE

    if 'link' in params:
        LFLAGS


    if arch == ARCH.ARM_M:
        EXEC_PATH = EXEC_PATH_ARM_M
        PREFIX = PREFIX_ARM_M
    elif arch == ARCH.ARM_A:
        EXEC_PATH = EXEC_PATH_ARM
        PREFIX = PREFIX_ARM
    elif arch == ARCH.AARCH64:
        EXEC_PATH = EXEC_PATH_AARCH64
        PREFIX = PREFIX_AARCH64
    
    compiler = {
        'EXEC_PATH' : EXEC_PATH,
        'CC'        : PREFIX + CC,
        'CFLAGS'    : CFLAGS,
        'CXX'       : PREFIX + CXX,
        'AS'        : PREFIX + AS,
        'AFLAGS'    : AFLAGS,
        'AR'        : PREFIX + AR,
        'LINK'      : PREFIX + LINK,
        'LFLAGS'    : LFLAGS,
        'SIZE'      : PREFIX + SIZE,
        'OBJDUMP'   : PREFIX + OBJDUMP,
        'OBJCPY'    : PREFIX + OBJCPY,
        'READELF'   : PREFIX + READELF,
    }

    # gcc = os.path.join(EXEC_PATH, compiler['CC'])
    # print('\n\n编译工具：%s\n\n版本信息：\n' % gcc)
    # os.system(gcc + ' -v')

    return compiler

# print(GetCompiler())
# print(GetCompiler(CPU.M0, bare = True))
# print(GetCompiler(CPU.M0, bare = 0))
# print(GetCompiler(CPU.A7, bare = 0))
# print(GetCompiler(CPU.M0, bare = 2))
# print(GetCompiler(CPU.M0, bare = 'sf'))
# print(GetCompiler(CPU.M0))
# print(GetCompiler(CPU.M0))
# print(GetCompiler(CPU.A53))
